<template>
  <div class="login-container">
    <el-row>
      <el-col :span="9">
        <div class="dark"></div>
      </el-col>
      <el-col :span="6">
        <div class="container">
          <el-form
            class="form"
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            autocomplete="on"
            label-position="left"
          >
            <div class="title-container">
<!--              <el-image-->
<!--                style="margin-bottom:30px;"-->
<!--                :fit="contain"-->
<!--                :src="require('@/../public/logo.png')"-->
<!--              ></el-image>-->
              <h3 class="title">
                图书管理系统
              </h3>
            </div>
            <el-form-item prop="username">
              <el-input
                clearable
                ref="username"
                v-model="loginForm.username"
                placeholder="用户名"
                prefix-icon="el-icon-user"
                name="username"
                type="text"
                tabindex="1"
                autocomplete="on"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                show-password
                clearable
                :key="passwordType"
                :type="passwordType"
                ref="password"
                v-model="loginForm.password"
                prefix-icon="el-icon-lock"
                placeholder="密码"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
            <el-button :loading="loading" type="primary" style="width:100%;"
                       @click.native.prevent="handleLogin"
            >
              登录
            </el-button>
          </el-form>
        </div>
      </el-col>
      <el-col :span="9">
        <div class="dark"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('~@/assets/background/login-background.jpg');
  background-size: cover;
}

.form {
  width: 80%;
  padding: 30px;
  margin: 0 auto;
  background: rgb(250, 250, 250);
  border-radius: 10px;
  text-align: center;
}

.title {
  font-size: 22px;
  color: #304156;
  margin: 0px auto 30px auto;
  text-align: center;
  font-weight: bold;
}

.set-language {
  color: #eee;
  position: absolute;
  top: 10%;
  font-size: 18px;
  right: 5%;
  cursor: pointer;
}

.dark {
  min-height: 36px;
}

.container {
  margin-top: 40%;
}
</style>
